package com.freshcity.upload
{
	import com.freshcity.upload.ui.CutBox;
	import com.freshcity.upload.ui.RectBox;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	
	public class LocalPicArea extends Sprite
	{
		public function LocalPicArea()
		{
			super();
			this.init();
		}
		
		private function stopResizeCutBox(arg1:MouseEvent):void
		{
			Mouse.show();
			this._cursorScale.visible = false;
			this._cutBox.addEventListener(MouseEvent.MOUSE_OVER, this.changeCursor);
			this.swfStage.removeEventListener(flash.events.Event.ENTER_FRAME, this.resizeCutBox);
			this.swfStage.removeEventListener(MouseEvent.MOUSE_UP, this.stopResizeCutBox);
			return;
		}
		
		private function addedToStage(arg1:flash.events.Event):void
		{
			this.swfStage = this.stage;
			var loc1:*=this.parent as CutView;
			this._avatar = loc1.avatarArea;
			this.removeEventListener(flash.events.Event.ADDED_TO_STAGE, this.addedToStage);
			return;
		}
		
		private function init():void
		{
			this._avatarBmp = new flash.display.Bitmap(null);
			this._cutAreaBg = new RectBox(this.MAX_WIDTH, this.MAX_HEIGHT);
			this._picContainer = new flash.display.Sprite();
			this._cursorMove = new SK_CursorMove();
			this._cursorMove.mouseEnabled = false;
			this._cursorMove.visible = false;
			this._cursorScale = new SK_CursorScale();
			this._cursorScale.mouseEnabled = false;
			this._cursorScale.visible = false;
			this.tip = new SK_Tip();
			this.tip.y = 1;
			this.tip.x = 1;
			
			this.createLoaddingUI();
			addChild(this._cutAreaBg);
			addChild(this.tip);
			addChild(this._picContainer);
			
			addChild(this.loaddingUI);
			this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.addedToStage);
			return;
		}
		
		private function createLoaddingUI():void
		{
			this.loaddingUI = new SK_Loading() as flash.display.MovieClip;
			this.loaddingUI.x = 151;
			this.loaddingUI.y = 130;
			this.loaddingUI.stop();
			this.loaddingUI.visible = false;
			return;
		}
		
		public function showTunBtns(arg1:Boolean):void
		{
			
		}
		
		public function setLocalPicSize(arg1:flash.display.BitmapData):void
		{
			var loc6:*=null;
			if (this.tip != null) 
			{
				removeChild(this.tip);
				this.tip = null;
			}
			
			this._sourceBMD = arg1;
			var loc1:*=new flash.display.Bitmap(arg1);
			var loc2:*=this.MAX_WIDTH / loc1.width;
			var loc3:*=this.MAX_HEIGHT / loc1.height;
			this._picRate = loc2 < loc3 ? loc2 : loc3;
			loc1.width = loc1.width * this._picRate;
			loc1.scaleY = loc1.scaleX;
			loc1.smoothing = true;
			if (loc2 < loc3) 
			{
				loc1.height = int(loc1.height);
			}
			else 
			{
				loc1.width = int(loc1.width);
			}
			while (this._picContainer.numChildren) 
			{
				loc6 = this._picContainer.removeChildAt((this._picContainer.numChildren - 1)) as flash.display.Bitmap;
				loc6 = null;
			}
			var loc7:*;
			var loc8:*;
			this._sourceBMD = loc7 = loc1.bitmapData;
			var loc4:*=loc7;
			var loc5:* = new flash.display.Bitmap(loc4);
			loc5.width = loc1.width;
			loc5.height = loc1.height;
			loc5.smoothing = true;
			this._picContainer.addChild(loc1);
			loc1.alpha = 0.5;
			this._picContainer.addChild(loc5);
			this._picContainer.x = loc7 = (this.MAX_WIDTH - loc1.width) * 0.5 + 1;
			this._picX = loc7;
			this._picContainer.y = loc8 = (this.MAX_HEIGHT - loc1.height) * 0.5 + 1;
			this._picY = loc8;
			this.addCutBox(loc5);
			this.showTunBtns(true);
		}
		
		public function addCutBox(arg1:flash.display.Bitmap):void
		{
			var loc1:*=NaN;
			var loc2:*=NaN;
			
			if (this._cutBox != null) 
			{
				removeChild(this._cutBox);
				this._cutBox = null;
			}
			
			loc1 = arg1.width < 110?arg1.width : 110;
			loc2 = arg1.height < 135?arg1.height : 135;
			
			this._cutBox = new CutBox(loc1, loc2);
			this._cutBox.x = this._picContainer.x + (this._picContainer.width - this._cutBox.cutArea.width) * 0.5;
			this._cutBox.y = this._picContainer.y + (this._picContainer.height - this._cutBox.cutArea.height) * 0.5;
			addChild(this._cutBox);
			addChild(this._cursorMove);
			addChild(this._cursorScale);
			arg1.mask = this._cutBox.maskArea;
			this._cutBox.addEventListener(MouseEvent.MOUSE_OVER, this.changeCursor);
			var loc3:*=this.getAvatarBMD(this._picX, this._picY, this._picRate, this._cutBox);
			this._avatar.changeAvatars(loc3);
			return;
		}
		
		private function changeCursor(arg1:MouseEvent):void
		{
			var loc1:*=arg1.target as flash.display.Sprite;
			if (loc1 != this._cutBox.cutArea) 
			{
				if (loc1 == this._cutBox.flag) 
				{
					Mouse.hide();
					this._cursorScale.visible = true;
					this._cursorMove.visible = false;
					this._cursorScale.x = this.mouseX;
					this._cursorScale.y = this.mouseY;
				}
			}
			else 
			{
				Mouse.hide();
				this._cursorMove.visible = true;
				this._cursorScale.visible = false;
				this._cursorMove.x = this.mouseX;
				this._cursorMove.y = this.mouseY;
			}
			this._cutBox.addEventListener(MouseEvent.MOUSE_MOVE, this.moveCursor);
			this._cutBox.addEventListener(MouseEvent.MOUSE_DOWN, this.changeCutBox);
			this._cutBox.addEventListener(MouseEvent.MOUSE_OUT, this.resetCursor);
			return;
		}
		
		private function resetCursor(arg1:MouseEvent):void
		{
			Mouse.show();
			this._cursorMove.visible = false;
			this._cursorScale.visible = false;
			this._cutBox.removeEventListener(MouseEvent.ROLL_OUT, this.resetCursor);
			this._cutBox.removeEventListener(MouseEvent.MOUSE_MOVE, this.moveCursor);
			this._cutBox.removeEventListener(MouseEvent.MOUSE_DOWN, this.changeCutBox);
			return;
		}
		
		private function moveCursor(arg1:MouseEvent):void
		{
			var loc2:*=null;
			var loc1:*=arg1.target as flash.display.Sprite;
			var loc3:*=loc1;
			switch (loc3) 
			{
				case this._cutBox.cutArea:
				{
					loc2 = this._cursorMove as flash.display.Sprite;
					break;
				}
				case this._cutBox.flag:
				{
					loc2 = this._cursorScale as flash.display.Sprite;
					break;
				}
			}
			this.cursorFollow(loc2);
			arg1.updateAfterEvent();
			return;
		}
		
		private function cursorFollow(arg1:flash.display.Sprite):void
		{
			arg1.x = this.mouseX;
			arg1.y = this.mouseY;
			return;
		}
		
		private function changeCutBox(arg1:MouseEvent):void
		{
			var loc1:*=arg1.target as flash.display.Sprite;
			var loc2:*=loc1;
			switch (loc2) 
			{
				case this._cutBox.cutArea:
				{
					this.swfStage.addEventListener(MouseEvent.MOUSE_MOVE, this.moveCutBox);
					this.swfStage.addEventListener(MouseEvent.MOUSE_UP, this.stopMoveCutBox);
					break;
				}
				case this._cutBox.flag:
				{
					this.flagVY = this.mouseY - this._cutBox.y - this._cutBox.maskArea.height;
					this.flagVX = this.mouseX - this._cutBox.x - this._cutBox.maskArea.width;
					this.flagCurX = this.mouseX;
					this.flagCurY = this.mouseY;
					this._cutBox.removeEventListener(MouseEvent.MOUSE_OVER, this.changeCursor);
					this.swfStage.addEventListener(flash.events.Event.ENTER_FRAME, this.resizeCutBox);
					this.swfStage.addEventListener(MouseEvent.MOUSE_UP, this.stopResizeCutBox);
					break;
				}
			}
			return;
		}
		
		private function moveCutBox(arg1:MouseEvent):void
		{
			this._cutBox.startDrag(false, new Rectangle(this._picContainer.x + 0.5, this._picContainer.y + 0.5, this._picContainer.width - this._cutBox.maskArea.width - 0.5, this._picContainer.height - this._cutBox.maskArea.height - 0.5));
			var loc1:*=this.getAvatarBMD(this._picX, this._picY, this._picRate, this._cutBox);
			this._avatar.changeAvatars(loc1);
			return;
		}
		
		private function stopMoveCutBox(arg1:flash.events.Event):void
		{
			this._cutBox.stopDrag();
			this.swfStage.removeEventListener(MouseEvent.MOUSE_MOVE, this.moveCutBox);
			this.swfStage.removeEventListener(MouseEvent.MOUSE_UP, this.stopMoveCutBox);
			return;
		}
		
		private function resizeCutBox(arg1:flash.events.Event):void
		{
			var loc1:*=NaN;
			var loc2:*=NaN;
			var loc3:*=NaN;
			var loc4:*=NaN;
			var loc5:*=null;
			Mouse.hide();
			this._cursorScale.x = this.mouseX;
			this._cursorScale.y = this.mouseY;
			this._cursorScale.visible = true;
			if (!(this.mouseX == this.flagCurX) || !(this.mouseY == this.flagCurY)) 
			{
				loc1 = this.mouseX - this._cutBox.x - this.flagVX;
				loc2 = this.mouseY - this._cutBox.y - this.flagVY;
				if (loc2 >= this._picContainer.y + this._picContainer.height - this._cutBox.y) 
				{
					loc2 = this._picContainer.y + this._picContainer.height - this._cutBox.y - 0.5;
				}
				if (loc2 < this._h) 
				{
					loc2 = this._h;
				}
				if (loc1 >= this._picContainer.x + this._picContainer.width - this._cutBox.x) 
				{
					loc1 = this._picContainer.x + this._picContainer.width - this._cutBox.x - 0.5;
				}
				if (loc1 < this._w) 
				{
					loc1 = this._w;
				}
				loc3 = loc1 - this._cutBox.maskArea.width;
				loc4 = loc2 - this._cutBox.maskArea.height;
				var scaleX:*;
				
				if (loc3 > loc4) 
				{
					scaleX = loc2/_cutBox.maskArea.height;
					this._cutBox.maskArea.height = loc2;
					this._cutBox.maskArea.width = _cutBox.maskArea.width*scaleX;
				}
				else 
				{
					scaleX = loc1/_cutBox.maskArea.height;
					this._cutBox.maskArea.height = loc1;
					this._cutBox.maskArea.width =  _cutBox.maskArea.width*scaleX;
				}
				this._cutBox.cutArea.width = this._cutBox.maskArea.width;
				this._cutBox.cutArea.height = this._cutBox.maskArea.height;
				this._cutBox.flag.x = this._cutBox.maskArea.width;
				this._cutBox.flag.y = this._cutBox.maskArea.height;
				loc5 = this.getAvatarBMD(this._picX, this._picY, this._picRate, this._cutBox);
				this._avatar.changeAvatars(loc5);
			}
			return;
		}
		
		private function getAvatarBMD(arg1:Number, arg2:Number, arg3:Number, arg4:CutBox):BitmapData
		{
			var w:*=arg4.maskArea.width / arg3;
			var loc2:*=110 / w;
			
			var h:*=arg4.maskArea.height /arg3;
			
			var loc3:BitmapData=new flash.display.BitmapData(110, 135, false);
			var loc4:*=int((arg4.x - arg1) / arg3);
			var loc5:*=int((arg4.y - arg2) / arg3);
			this._avatarBmp.bitmapData = this._sourceBMD;
			this._avatarBmp.smoothing = true;
			loc3.draw(this._avatarBmp, new Matrix(loc2, 0, 0, loc2, (-loc4) * loc2, (-loc5) * loc2), null, null, new Rectangle(0, 0, w * loc2, h * loc2), false);
			return loc3;
		}
		
		private function changeBmdRight(arg1:flash.display.BitmapData):flash.display.BitmapData
		{
			var loc1:*=arg1;
			var loc2:*=loc1.width;
			var loc3:*=loc1.height;
			var loc4:*=new flash.display.BitmapData(loc3, loc2, false);
			var loc5:*;
			(loc5 = new Matrix()).rotate(Math.PI * 0.5);
			loc5.translate(loc3, 0);
			loc4.draw(loc1, loc5);
			return loc4;
		}
		
		private function changeBmdLeft(arg1:flash.display.BitmapData):flash.display.BitmapData
		{
			var loc1:*=arg1;
			var loc2:*=loc1.width;
			var loc3:*=loc1.height;
			var loc4:*=new flash.display.BitmapData(loc3, loc2, false);
			var loc5:*;
			(loc5 = new Matrix()).rotate((-Math.PI) * 0.5);
			loc5.translate(0, loc2);
			loc4.draw(loc1, loc5);
			return loc4;
		}
		
		private function rotationPicLeft(arg1:MouseEvent):void
		{
			this._sourceBMD = this.changeBmdLeft(this._sourceBMD);
			this.setLocalPicSize(this._sourceBMD);
			return;
		}
		
		private function rotationPicRight(arg1:MouseEvent):void
		{
			this._sourceBMD = this.changeBmdRight(this._sourceBMD);
			this.setLocalPicSize(this._sourceBMD);
			return;
		}
		
		private const MAX_WIDTH:Number=300;
		
		private const MAX_HEIGHT:Number=300;
		
		public var loaddingUI:flash.display.MovieClip;
		
		public var tip:SK_Tip;
		
		private var swfStage:flash.display.Stage;
		
		private var flagVY:Number;
		
		private var _avatarBmp:flash.display.Bitmap;
		
		private var flagCurX:Number;
		
		private var flagCurY:Number;
		
		private var _h:Number=10;
		
		private var _w:Number=10;
		
		private var _cutAreaBg:RectBox;
		
		private var _cursorMove:SK_CursorMove;
		
		private var _cursorScale:SK_CursorScale;
		
		private var _sourceBMD:flash.display.BitmapData;
		
		private var _picRate:Number;
		
		private var _picX:Number;
		
		private var _picY:Number;
		
		private var _avatar:AvatarArea;
		
		private var _picContainer:flash.display.Sprite;
		
		private var _cutBox:CutBox;
		
		private var flagVX:Number;		
	}
}